/*	Interactive Gravitational Simulator
	Project: IGS Visualization
	File: Application.h
	Author: Mike Bantegui <mbante2@gmail.com>, Hofstra University 
	Copyright (C) 2012 - 2013 Mike Bantegui

	This file is part of the IGS software package for simulating
	the N-Body problem in real-time. This was developed as part
	of a departmental honors thesis project at Hofstra University
	in the Spring 2012 semester.

	IGS is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	IGS is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef IGS_APPLICATION_H
#define IGS_APPLICATION_H

#include "FrameCounter.h"
#include <SDL.h>
#include <vector>

namespace IGS
{

class Application
{
private:
	// The thread responsible for running Loop 
	SDL_Thread *LoopThread;

	// Whether SDL has been initialized
	bool IsSDLInitialized;
	// Whether the Application is running
	bool IsRunning;
	// Whether the Application is visible
	bool IsVisible;

	// The screen for the Application window
	SDL_Surface *Screen;
	// The width of the Application window
	unsigned _Width;
	// The height of the Application window
	unsigned _Height;

	// The field of view
	double FoV;
	// The near clipping plane
	double Zmin;
	// The far clipping plane
	double Zmax;

protected:
	// The timer used to compute frame rate
	FrameCounter<unsigned()> DrawTimer;
	// The timer used to time the code in Loop()
	FrameCounter<unsigned()> LoopTimer;

	// The most recent key events
	std::vector<SDL_Event> KeyEvents;
	// Whether each key has been pressed or not
	bool KeyState[SDLK_LAST];

public:
	Application();
	virtual ~Application() = 0;

	bool Running() const;
	bool Visible() const;
	unsigned Width() const;
	unsigned Height() const;
	double Aspect() const;

	bool Start(int argc, char **argv);
	void Quit();
	void Run();

private:
	bool InitializeSDL();
	void PollEvents();

protected:
	virtual bool Initialize(int argc, char **argv) = 0;
	virtual void Cleanup() = 0;
	virtual void Draw() = 0;
	virtual void Keys() = 0;
	virtual void Mouse() = 0;
	virtual void Loop() = 0;

	void Set2DContext();
	void Set3DContext();

	friend int LoopFunc(void *data);
};

}

#endif